package com.oa.action;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.oa.entity.Reply;
import com.oa.entity.Topic;
import com.oa.entity.User;
import com.oa.service.ReplyService;
import com.oa.service.TopicService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller("replyAction")
@Scope("prototype")
public class ReplyAction extends ActionSupport implements ModelDriven<Reply> {

	private static final long serialVersionUID = 1L;
	@Resource(name="replyService")
	private ReplyService replyService;
	@Resource(name="topicService")
	private TopicService topicService;

	private Reply reply;
	private String queryDetailTopicUrl;
	private String topicId;

	public String getTopicId() {
		return topicId;
	}

	public void setTopicId(String topicId) {
		this.topicId = topicId;
	}

	public String getQueryDetailTopicUrl() {
		return queryDetailTopicUrl;
	}

	public void setQueryDetailTopicUrl(String queryDetailTopicUrl) {
		this.queryDetailTopicUrl = queryDetailTopicUrl;
	}

	public Reply getReply() {
		return reply;
	}

	public void setReply(Reply reply) {
		this.reply = reply;
	}

	@Override
	public Reply getModel() {
		if (reply == null)
			reply = new Reply();
		return reply;
	}

	// 添加回复
	public String addReply() {
		ActionContext act = ActionContext.getContext();
		User postingAuthor = (User) act.getSession().get("user1");
		queryDetailTopicUrl = act.getSession().get("queryDetailTopicUrl")
				.toString();
		reply.setPostingAuthor(postingAuthor);
		Topic topic = topicService.selectTopicName(reply.getPostingTitle());
		reply.setTopic(topic);
		reply.setPostingTitle(reply.getPostingTitle().trim());
		replyService.addReply(reply);
		return "addReply";
	}
	

	// 删除回复
	public String deleteReply() {
		
		Reply replyNext = replyService.selectReply(reply.getPostingId());
		replyService.deleteReply(replyNext);
		Reply lastReply = replyService.selectLastReply(topicId);
		Topic topic = topicService.selectTopic(topicId);
		topic.setLastReply(lastReply);
		topic.setLastUpdateTime(lastReply.getPostingTime());
		topicService.updateTopic(topic);
		queryDetailTopicUrl = ActionContext.getContext().getSession()
				.get("queryDetailTopicUrl").toString();
		return "deleteReply";
	}
	
	
	

}
